package common

import (
	"context"
	"fmt"
	"ginpro/app/pkg/logger"
	"ginpro/app/pkg/redis"
	"ginpro/app/rule"

	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type AppService struct {
	Rule  *gin.Engine
	Log   *zap.SugaredLogger
	Redis *redis.RedisClient
}

func LoadApp() *AppService {
	//1 加载路由
	//1 加载数据库
	//2 加载redis
	//3 加载日志
	SetupLogging()
	return &AppService{
		Rule:  rule.Router(),
		Redis: newRedis(),
		Log:   SetupLogging(),
	}
}

// 加载数据库
func database() {

}

// 加载 redis
func newRedis() *redis.RedisClient {
	// 初始化 Redis 客户端
	redisClient := redis.NewRedisClient("127.0.0.1", "3306", 6379, 1)
	// 测试连接
	ctx := context.Background()
	if err := redisClient.Client.Ping(ctx); err != nil {
		logger.SugarLogger.Fatal(fmt.Sprintf("Redis connect error: %s", err))
	}
	return redisClient
}

// 加载redis
func SetupLogging() *zap.SugaredLogger {
	logger.InitLogger("./logs")
	defer logger.SugarLogger.Sync()
	return logger.SugarLogger
}
